package org.newlifecac;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Properties;
import org.newlifecac.util.NetworkDetector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ToolsActivity extends Activity implements OnClickListener
{
	private Gallery g = null;
	private TextView  tv_fraction;
	private TextView tv_demom;
	private Button btnback;
	private Button btnStop;
	
	private List<String> it = null;
	public String actionUrl = null;
	private int count_drawble = 0;
	private int cur_index = 0;
	private boolean isalive = true;
	private boolean flag = true;
	private static int MSG_UPDATE = 1;
	private static String SD_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
	
	public static File PIC_PATH = new File(SD_PATH + "/Picture/");
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_photo);
		
		tv_demom = (TextView) findViewById(R.id.tv_demom);
		tv_fraction = (TextView) findViewById(R.id.tv_fraction);
		btnback = (Button) findViewById(R.id.btnback);
		btnStop = (Button) findViewById(R.id.btnStop);
		btnback.setOnClickListener(this);
		btnStop.setOnClickListener(this);
		
		g = (Gallery) findViewById(R.id.mygallery);
		g.setAdapter(new ImageAdapter(this, getSD()));
		count_drawble = new ImageAdapter(this, getSD()).getCount();
		g.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View v, int position, long id)
			{
				Toast.makeText(ToolsActivity.this, (position + 1) + "\n" + it.get(position), Toast.LENGTH_LONG).show();
			}
		});
		tv_demom.setText(count_drawble+"");
		
		new Thread(new Runnable()
		{
			public void run()
			{
				while (isalive && flag)
				{
					cur_index = cur_index % count_drawble;
					
					Message msg = mhandler.obtainMessage(MSG_UPDATE, cur_index, 0); 
					mhandler.sendMessage(msg);
					
					try
					{
						Thread.sleep(2000);
					} catch (Exception e)
					{
						e.printStackTrace();
					}
					cur_index++;
				}
				if (!flag) return;
			}
		}).start();
	}
	
	
	
	
	
	
	
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.btnback:
			onKeyDown(KeyEvent.KEYCODE_BACK, null);
			break;

		case R.id.btnStop:
			if (btnStop.getText().toString().equals("Stop"))
			{
				flag = false;
				btnStop.setText("Go");
			} else if(btnStop.getText().toString().equals("Go"))
			{
				flag = true;
				btnStop.setText("Stop");
				new Thread(new Runnable()
				{
					public void run()
					{
						while (isalive && flag)
						{
							cur_index = cur_index % count_drawble;
							
							Message msg = mhandler.obtainMessage(MSG_UPDATE, cur_index, 0); 
							mhandler.sendMessage(msg);
							
							try
							{
								Thread.sleep(2000);
							} catch (Exception e)
							{
								e.printStackTrace();
							}
							cur_index++;
						}
						if (!flag) return;
					}
				}).start();
				
			}
			break;
		}
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK )
		{
			System.out.println("onKeydown");
			Intent intent = new Intent();
			intent.setClass(ToolsActivity.this, FilePickerActivity.class);
			startActivity(intent);
			ToolsActivity.this.finish();
		}
		return false;
	}

	
	
	private Handler mhandler = new Handler()
	{
		public void handleMessage(Message msg) 
		{
			if (msg.what == MSG_UPDATE)
			{
				g.setSelection(msg.arg1);
			}
			tv_fraction.setText((cur_index+1)+"/");
		}
	};
	
	private List<String> getSD()
	{
		it = new ArrayList<String>();
		File f = new File(SD_PATH + "//" + "Picture");
		File[] files = f.listFiles();
		for (int i = 0; i < files.length; i++)
		{
			File file = files[i];
			if (getImageFile(file.getPath()))
				it.add(file.getPath());
		}
		return it;
	}
	
	private boolean getImageFile(String fName)
	{
		boolean re;
		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();
		if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp"))
		{
			re = true;
		} else
		{
			re = false;
		}
		return re;
	}
	
	public class ImageAdapter extends BaseAdapter
	{
		int mGalleryItemBackground;
		private Context mContext;
		private List<String> lis;

		public ImageAdapter(Context c, List<String> li)
		{
			mContext = c;
			lis = li;
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
			mGalleryItemBackground = a.getResourceId(
					R.styleable.Gallery_android_galleryItemBackground, 0);
			a.recycle();
		}

		public int getCount()
		{
			return lis.size();
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			ImageView i = new ImageView(mContext);
			
			Bitmap bm = BitmapFactory.decodeFile(lis.get(position).toString());
			i.setImageBitmap(bm);
			i.setScaleType(ImageView.ScaleType.CENTER);
			i.setLayoutParams(new Gallery.LayoutParams(1024, 768));
			i.setBackgroundResource(mGalleryItemBackground);
			return i;
		}
	}

	public void Toast(String str)
	{
		Toast.makeText(ToolsActivity.this, str,Toast.LENGTH_LONG).show();
	}
}
